const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    status_list: ['全部', '未结算', '已结算', '已失效'],
    current_index: 0,
    info:{},

    language:"",
    list: [],
    amounts_list:[],
    page: 1,
    total_page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
    this.get_rewards();
  },
  get_rewards(page = 1) {
    let json = {
      page,
      status:this.data.current_index
    }
    if(this.data.current_index == 0){
      json.status = 4;
    }
    app._postNetWork({
      url:app.getNetAddresss("plugin.distributor-team.frontend.controllers.rewards.index"),
      data:json,
      success:resdata=>{
        let {result,msg,data}  = resdata.data;
        if(result){
          let {info,list} = data;
          this.data.page = list.current_page;
          this.data.total_page = list.last_page;
          wx.setNavigationBarTitle({
            title: info.plugin_name || '经销商团队奖励',
          })
          if(page == 1){
            this.setData({
              list:list.data,
              info
            })
          }else{
            this.setData({
              list:this.data.list.concat(list.data)
            })
          }
        }else{
          app.tips(msg);
        }
      }
    })
  },
  get_amounts(page=1){
    app._postNetWork({
      url:app.getNetAddresss("plugin.distributor-team.frontend.controllers.amounts.index"),
      data:{page},
      success:resdata=>{
        let {result,msg,data}  = resdata.data;
        if(result){
          let {info,list} = data;
          this.data.page = list.current_page;
          this.data.total_page = list.last_page;
          if(page == 1){
            this.setData({
              amounts_list:list.data,
              info
            })
          }else{
            this.setData({
              amounts_list:this.data.amounts_list.concat(list.data)
            })
          }
        }else{
          app.tips(msg);
        }
      }
    })
  },
  getMoreData() {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    let page = this.data.page + 1;
    if(this.data.active == 0){
      this.get_rewards(page)
    }else{
      this.get_amounts(page)
    }
  },
  toggle(e) {
    this.setData({current_index:e.currentTarget.dataset.i});
    this.get_rewards()
  },
  onClick(event){
    this.setData({active:event.detail.index});
    if(event.detail.index==0){
      this.get_rewards()
    }else{
      this.get_amounts()
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMoreData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})